import requests
import json
import re
import sys

def get_wikiquote_quotes(person, limit=50):
    base_url = "https://en.wikiquote.org/w/api.php"
    
    # Step 1: Get the page ID
    search_params = {
        "action": "query",
        "format": "json",
        "list": "search",
        "srsearch": person
    }
    response = requests.get(base_url, params=search_params).json()
    
    if "query" not in response or not response["query"]["search"]:
        print("No results found for", person)
        return []
    
    page_title = response["query"]["search"][0]["title"]
    
    # Step 2: Get the page content in JSON format
    content_params = {
        "action": "query",
        "format": "json",
        "prop": "extracts",
        "explaintext": True,
        "titles": page_title
    }
    content_response = requests.get(base_url, params=content_params).json()
    
    pages = content_response.get("query", {}).get("pages", {})
    if not pages:
        print("Failed to retrieve content for", person)
        return []
    
    page_content = next(iter(pages.values())).get("extract", "")
    
    # Step 3: Extract quotes (lines longer than 150 characters)
    quotes = [line for line in page_content.split("\n") if len(line) > 150]
    
    return quotes[:limit]  # Limit number of quotes returned


def save_quotes_to_file(person, quotes):
    filename = f"{person.replace(' ', '_')}_quotes.txt"
    with open(filename, "w", encoding="utf-8") as file:
        for quote in quotes:
            file.write(f"{quote}\n\t\t-- {person}\n%\n")
    print(f"Saved {len(quotes)} quotes to {filename}")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python script.py \"Person Name\"")
        sys.exit(1)
    
    person = sys.argv[1]
    quotes = get_wikiquote_quotes(person)
    
    if quotes:
        save_quotes_to_file(person, quotes)
    else:
        print("No quotes found.")
